package cn.cttic.base.service.impl;

import cn.cttic.base.api.domain.BaseProduct;
import cn.cttic.base.api.domain.vo.BaseCompanyVo;
import cn.cttic.base.api.domain.vo.BaseProductVo;
import cn.cttic.base.service.IBaseCompanyService;
import cn.cttic.base.service.IBaseProductService;
import cn.cttic.common.core.utils.MapstructUtils;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cttic.base.api.domain.bo.BaseCompanyProductBo;
import cn.cttic.base.api.domain.vo.BaseCompanyProductVo;
import cn.cttic.base.api.domain.BaseCompanyProduct;
import cn.cttic.base.mapper.BaseCompanyProductMapper;
import cn.cttic.base.service.IBaseCompanyProductService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 企业套餐Service业务层处理
 *
 * @author Times.Studio
 * @date 2024-09-17
 */
@RequiredArgsConstructor
@Service
public class BaseCompanyProductServiceImpl implements IBaseCompanyProductService {

    private final BaseCompanyProductMapper baseMapper;

    private final IBaseCompanyService baseCompanyService;

    private final IBaseProductService baseProductService;

    /**
     * 查询企业套餐
     *
     * @param id 主键
     * @return 企业套餐
     */
    @Override
    public BaseCompanyProductVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询企业套餐列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 企业套餐分页列表
     */
    @Override
    public TableDataInfo<BaseCompanyProductVo> queryPageList(BaseCompanyProductBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseCompanyProduct> lqw = buildQueryWrapper(bo);
        Page<BaseCompanyProductVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        List<BaseCompanyProductVo> voList = result.getRecords();
        voList.forEach( bcp -> {
            if (StrUtil.isNotBlank(bcp.getCompanyId())) {
                BaseCompanyVo companyVo = baseCompanyService.queryById(bcp.getCompanyId());
                if (companyVo != null) {
                    bcp.setCompanyName(companyVo.getCompanyName());
                }
            }
            if (StrUtil.isNotBlank(bcp.getProductId())) {
                BaseProductVo productVo = baseProductService.queryById(bcp.getProductId());
                if (productVo != null) {
                    bcp.setProductName(productVo.getProductName());
                }
            }
        });
        result.setRecords(voList);

        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的企业套餐列表
     *
     * @param bo 查询条件
     * @return 企业套餐列表
     */
    @Override
    public List<BaseCompanyProductVo> queryList(BaseCompanyProductBo bo) {
        LambdaQueryWrapper<BaseCompanyProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseCompanyProduct> buildQueryWrapper(BaseCompanyProductBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseCompanyProduct> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(BaseCompanyProduct::getCreateTime);
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyId()), BaseCompanyProduct::getCompanyId, bo.getCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getProductId()), BaseCompanyProduct::getProductId, bo.getProductId());
        lqw.eq(bo.getSalePrice() != null, BaseCompanyProduct::getSalePrice, bo.getSalePrice());
        lqw.eq(StringUtils.isNotBlank(bo.getCarModel()), BaseCompanyProduct::getCarModel, bo.getCarModel());
        return lqw;
    }

    /**
     * 新增企业套餐
     *
     * @param bo 企业套餐
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseCompanyProductBo bo) {
        BaseCompanyProduct add = MapstructUtils.convert(bo, BaseCompanyProduct.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改企业套餐
     *
     * @param bo 企业套餐
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseCompanyProductBo bo) {
        BaseCompanyProduct update = MapstructUtils.convert(bo, BaseCompanyProduct.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseCompanyProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除企业套餐信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
